#include "esp12s.h"
#include "tim.h"
#include "main.h"
#include "FreeRTOS.h"
wifiRunStatusEnum               wifiRun = WIFI_INIT;
wifiRunEnum						          wifiRunStatus = WIFI_POWER_ON;
static uint8_t					        receiveReturnBuff[50] , receiveReturnBuffLeng;



__IO uint8_t timeflg=0;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  if (htim->Instance == TIM1) {
    HAL_IncTick();
  }
  if (htim->Instance == TIM6) {
		 timeflg=1;
  }
}
void esp12s_resert( void )
{
	printf("esp12s_resert0------------------------->\n");
	HAL_GPIO_WritePin(GPIOB, WIFIPOWER_Pin, GPIO_PIN_RESET);
	HAL_Delay(10);
	HAL_GPIO_WritePin(GPIOB, WIFIPOWER_Pin, GPIO_PIN_SET);
	HAL_Delay(500);
	printf("esp12s_resert1------------------------->\n");
	__HAL_UART_CLEAR_IDLEFLAG(GKU2.huart);
	GKU2.rx_flag=0;
}

void esp12s_init(void)
{
//USART2:115200 模型
//PB2 拉高
//2个全局变量
	HAL_GPIO_WritePin(GPIOB, WIFIPOWER_Pin, GPIO_PIN_SET);
	wifiRunStatus = WIFI_POWER_OFF; 
	wifiRun = WIFI_INIT;
}
#define max(x,y) x>y?x:y
uint8_t wifi_send_receive(GK_USARTIDLE_TYPE  *myuart, uint8_t *TX ,uint16_t TXlen ,uint8_t *RX,uint16_t *RXlen)
{

	uint8_t Cnt = 10;
	timeflg=0;
	GK_usart_clear(&GKU2);
	HAL_TIM_Base_Stop_IT(&htim6);
	HAL_TIM_Base_Start_IT(&htim6);
	HAL_UART_Transmit(myuart->huart, TX, TXlen, 0xFFFF);
	while(timeflg==0)//0的时候一直在这里毒蛇
	{	HAL_Delay(10);}
	
	HAL_TIM_Base_Stop_IT(&htim6);
	*RXlen=GKU2.rx_len;
	memcpy(RX,GKU2.rxBuf,max(GKU2.rx_len,100));
	
	return 1; 	
}



//WIFI第一阶段的封装！
//发送出去uint8_t *cmd , uint16_t length  超时uint16_t timeout 尝试次数 uint8_t repert 返回来的数据一定要有的关键数据checkData
uint8_t wifi_send_command(uint8_t *cmd , uint16_t length ,char *checkData)
{
	uint8_t back[100]={0};
	uint16_t len=0;


	if( wifi_send_receive(&GKU2, cmd , length ,back,&len) )
	{
		for(int i=0;i<len;i++)printf("#######%c ",back[i]);
		if( strstr((char *)back, checkData) != NULL)
		{
			return TRUE;
		}		
		memset(&back , 0x00 , sizeof(back));			
	}	

	return FALSE;

}

uint16_t wifi_wait_data(  uint16_t timeout ,uint8_t *respone)
{
  

}


//uint8_t wifi_send_receive_repeat(uint8_t *cmd , uint16_t length ,  uint16_t timeout , uint8_t repert ,usartReceiveDataType *respone )
//{

//}

uint8_t wifi_set_ssid( uint8_t *ssid , uint8_t *pwd )
{

}


uint8_t wifi_set_connect_ap( ) 
{
 
}


uint8_t wifi_set_default( void )
{


}


uint8_t wifi_check_power_on( void )
{
	char *cmd="AT\r\n";
	char len=strlen(cmd);
	char *targ="\r\nOK\r\n";
	
	if ( wifi_send_command((uint8_t *)cmd ,len ,targ)==TRUE)
	{          
		printf("WIFI AT OK\n");
		return TRUE;
	}

	printf("WIFI AT FAIL\n");
	return FALSE;    
  
}


uint8_t esp12s_read_connect_status( void )
{


}



void esp12s_start_init( void )
{
	//printf("%s\r\n",__FUNCTION__);
	uint16_t len;
	uint8_t back[100];
  switch(wifiRunStatus)
    {
        case WIFI_POWER_OFF:
        {
            esp12s_resert();
            wifiRunStatus = WIFI_POWER_ON;
        }break;
    	case WIFI_POWER_ON:
        {
				//GK_usart_TXRX(&GKU2, "AT\r\n" , strlen("AT\r\n" ) ,back,&len);
         wifi_assert(wifi_check_power_on() , WIFI_SET_ECHO);
        }break;
        case WIFI_SET_ECHO:
        {
					while(1)
					printf("1");
				}
//            if ( wifi_send_command("ATE0\r\n" , strlen("ATE0\r\n") , 500 ,20 , "\r\nOK\r\n") == TRUE )
//            {          
//                log(DEBUG,"WIFI设置回显成功\n");
//                wifiRunStatus = WIFI_SET_DEFAULT;
//            }
//            else
//            {
//                log(INFO,"WIFI设置回显失败\n");
//                wifiRunStatus =  WIFI_POWER_OFF; 
//            }
//        }break;

//    	case WIFI_SET_DEFAULT:
//        {
//            uint32_t restoreBit = 0;

//            restoreBit = config.read(CFG_SET_RESTORE_FLAG , NULL);

//            if( restoreBit & SYS_NET_RESTORE_BIT)
//            {
//                if( wifi_set_default() == TRUE )
//                {
//                    uint32_t bit =  SYS_NET_RESTORE_BIT;
//                    config.write(CFG_CLEAR_RESTORE_FLAG , &bit , TRUE );
//                    wifiRunStatus = WIFI_WAIT_CONNECT;
//                }
//                else   
//                {
//                    wifiRunStatus = WIFI_POWER_OFF;
//                }
//            }
//            else
//            {
//                wifiRunStatus = WIFI_WAIT_CONNECT;
//            }
//            
//        }break;
//    	case WIFI_WAIT_CONNECT:
//        {
//			uint32_t timeout = 30;

//			while( --timeout != 0 )
//			{				
//                if( esp12s_read_connect_status() <  WIFI_DISCONNECT_AP )
//                {
//                    wifiRunStatus = WIFI_WAIT_GOT_IP;
//                    return ;
//                }
//                
//                sys_delay(1000);
//			}

//			wifiRunStatus = WIFI_POWER_OFF;
//			
// 
//        }break;
//    	case WIFI_WAIT_GOT_IP:
//        {

//            usartReceiveDataType respone;

//			memset(&respone , 0x00 , sizeof(usartReceiveDataType) );
//			
//			if( wifi_send_receive_repeat("AT+CIFSR\r\n" , strlen("AT+CIFSR\r\n") , 1000 , 1 , &respone) == FALSE)
//			{
//				log(INFO,"获取WIFI模块IP信息错误\n");
//				wifiRunStatus = WIFI_POWER_OFF;

//				return;
//			}

//			log(DEBUG,"wifi 模块IP信息 : \n%s\n" , respone.rxBuff);

//			wifiRunStatus = WIFI_SET_MUX;
//  
//			
//        }break;

//        case WIFI_SET_MUX:
//        {
//            if ( wifi_send_command("AT+CIPMUX=1\r\n" , strlen("AT+CIPMUX=1\r\n") , 500 ,20 , "\r\nOK\r\n") == TRUE )
//            {          
//                log(INFO,"WIFI设置多连接模式成功\n");
//                wifiRunStatus = WIFI_INIT_FINSG;
//            }
//            else
//            {
//                log(INFO,"WIFI设置多连接模式失败\n");
//                wifiRunStatus =  WIFI_POWER_OFF; 
//            }
//        }break;
//    	case WIFI_INIT_FINSG:
//        {
//            log(DEBUG,"WIFI初始化完成，可以建立tcp连接\n");
//            wifiRunStatus = WIFI_INIT_SUCCESS;
//        }break;
//        case WIFI_INIT_SUCCESS:
//        {
//            wifiRun = WIFI_RECEIVE;
//            socket_set_status(SOCKET_WORKING_STATUS);
//        }break;

//        default: log(WARN,"wifi init no this handle = %d\n" , wifiRunStatus); wifiRunStatus = WIFI_POWER_OFF; break;
    }


}


static void wifi_rx_data_analisys(char *str , int len)
{

}


void wifi_module_receive( void )
{


}




uint8_t	esp12s_isOK( void )
{

}


int8_t esp12s_disconnect( int8_t id )
{

}


int8_t esp12s_connect( int8_t id , uint8_t *ip , uint16_t port)
{

 
}

int8_t esp12s_send( uint8_t id , uint8_t *sendData , uint16_t length )
{


}

void esp12s_close( void )
{


}

	uint16_t len;
	uint8_t back[100];
void StartTask03(void const * argument)
{
	static int once=1;
	esp12s_init();
	for(;;)
	{
		switch(wifiRun)
		{
			case WIFI_INIT:
			{
				//esp12s_start_init();
						//在这里断点！watchGKU2看到DMA已经过来很多数据！
				if(once)
				{once=0;
					esp12s_resert();
					GK_usart_clear(&GKU2);
					wifi_send_receive(&GKU2, "AT+RST\r\n" ,8 ,back,&len);
					printf("%s\r\n",back);
					GK_usart_clear(&GKU2);
					wifi_send_receive(&GKU2, "AT+CWMODE=?\r\n" ,13 ,back,&len);
					printf("%s\r\n",back);
					GK_usart_clear(&GKU2);
				}
			}break;
			case WIFI_RECEIVE:
			{
				wifi_module_receive();
			}break;
			default:printf("wifi run no this handle = %d\n" , wifiRun); wifiRun = WIFI_INIT; break;
		} 
	HAL_Delay(100);
	}
}
